package com.sykj.bsc.rwl.commons;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;

//注解的意思：只传送有数据的不会传送null, 例如:status有数据而data没不会传送
//保证json序列化的时候，如果是null的对象，key也会消失
@JsonSerialize(include = Inclusion.NON_NULL)
public class ServerResponse<T> implements Serializable{
	
	private int status;//状态
	private String msg;//消息
	private T data;
	
	private ServerResponse(int status){
		this.status=status;
	}
	private ServerResponse(int status,String msg){
		this.status=status;
		this.msg=msg;
	}
	private ServerResponse(int status,String msg,T data){
		this.status=status;
		this.msg=msg;
		this.data=data;
	}
	private ServerResponse(int status,T data){
		this.status=status;
		this.data=data;
	}
	
	public int getStatus() {
		return status;
	}
	public String getMsg() {
		return msg;
	}
	public T getData() {
		return data;
	}
	
	//不在json序列化结果当中
	@JsonIgnore
	public boolean isSuccess() {
		return this.status==ResponseCode.SUCCESS.getCode();
	}
	//成功
	public static <T> ServerResponse<T> createBySuccess(){
		return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
	}
	
	public static <T> ServerResponse<T> createBySuccess(String msg){
		return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
	}
	
	public static <T> ServerResponse<T> createBySuccess(T data){
		return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
	}
	public static <T> ServerResponse<T> createBySuccess(String msg,T data){
		return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
	}
	
	//同一错误
	public static <T> ServerResponse<T> createByError(){
		return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
	}
	//返回一个自定义的错误 例如用户名已存在
	public static <T> ServerResponse<T> createByError(String errormsg){
		return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errormsg);
	}
	
	//code和msg作为变量 返回错误 例如参数错误，未登录等等信息
	public static <T> ServerResponse<T> createByError(int code,String errormsg){
		return new ServerResponse<T>(code,errormsg);
	}
	
	
	
}
